package com.mylove.judas.yaya.retrofit;

import com.mylove.judas.yaya.BuildConfig;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Judas on 2018/4/25.
 */

public class ServiceFactory {

    public static <T> T createService(String base_url, Class<T> cls){

        OkHttpClient httpClient = new OkHttpClient();

        if (BuildConfig.DEBUG) { //网络请求日志开关, debug模式开启,
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
        }

        Retrofit adapter = new Retrofit.Builder().baseUrl(base_url)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        return adapter.create(cls);
    }

}
